package com.jkl.webserver.Web.Controller;

import com.jkl.webserver.Tool.xFile;
import com.jkl.webserver.Web.DAO.pojo.RDynamic;
import com.jkl.webserver.Web.DAO.pojo.REvaluation;
import com.jkl.webserver.Web.Service.WebIndexService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class WebIndexController {
    public final WebIndexService webIndexService;
    @Autowired
    public WebIndexController(WebIndexService webIndexService, xFile xfile) {
        this.webIndexService = webIndexService;
    }

    @GetMapping("/Index")
    public String indexPage(Model model, HttpSession session,HttpServletRequest request){
        String email=(String)session.getAttribute("email");
        if(email==null){
            return "redirect:/Login";
        }
        String username=(String)session.getAttribute("username");
        model.addAttribute("email",email);
        model.addAttribute("username",username);
        model.addAttribute("Web",getLocalHostAddress(request));
        model.addAttribute("basePath","/Index");
        return "views/index/index";
    }

    @GetMapping("/Index/exit")
    public String exit(HttpSession session){
        session.invalidate();
        return "redirect:/Login";// 重定向到登录页面
    }

    @GetMapping("/Index/img")
    public  void getSelfImgRes(@RequestParam String name, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String email= (String) request.getSession().getAttribute("email");
        String targetPath = null;
        switch (name) {
            case "get_head_img" : {
                {//获取用户头像
                    String user=request.getParameter("user");
                    if(user==null){
                        targetPath="/userRes/" + email + "/head_img/head.png";
                    }
                    else {
                        targetPath="/userRes/" + user+ "/head_img/head.png";
                    }
                    webIndexService.downloadFile(targetPath, response);
                }
            }break;
            case "background" : {//获取用户的背景图
                targetPath="/userRes/" + email + "/background/index_bkg.png";
                webIndexService.downloadFile(targetPath, response);
            }break;
            case "background_repeat" : {//获取用户的背景图的铺平图
                targetPath ="/userRes/" + email + "/background/index_bkgr.png";
            }break;
            case "com_collection" : {
                targetPath="/publicBackground/com_collection.png";
            }break;
            case "_com_collection" : {
                targetPath="/publicBackground/_com_collection.png";
            }break;
            case "detail_collection" : {
                targetPath="/publicBackground/detail_collection.png";
            }break;
            case "_detail_collection" : {
                targetPath="/publicBackground/_detail_collection.png";
            }break;
            case "nav_collection" : {
                targetPath="/publicBackground/nav_collection.png";
            }break;
            case "_nav_collection" : {
                targetPath="/publicBackground/_nav_collection.png";
            }break;
            case "nav_detail_collection" : {
                targetPath="/publicBackground/nav_detail_collection.png";
            }break;
            case "_nav_detail_collection" : {
                targetPath="/publicBackground/_nav_detail_collection.png";
            }break;
            case "adornmentBG" : {
                targetPath="/publicBackground/nav/adornment/bkg.png";
            }break;
            case "dynamicImg" : {//获取动态的用户图片
                String dynamicPath = request.getParameter("dynamicPath");
                String[] _name = dynamicPath.split("_");
                String imgName = request.getParameter("imgName");
                targetPath="/userRes/" + _name[0] + "/userDynamic/" + dynamicPath + "/img/" + imgName;
            }break;
            case "commentImg" : {//获取评论的用户图片
                String dynamicPath = request.getParameter("dynamicPath");
                String[] _name = dynamicPath.split("_");
                String commentPath = request.getParameter("commentPath");
                String imgName = request.getParameter("imgName");
                targetPath="/userRes/" + _name[0] + "/userDynamic/" + dynamicPath + "/evaluate/" + commentPath + "/img/" + imgName;
            }break;
            default :
                // 如果name不匹配任何已知值，返回错误响应
                    response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        }
        webIndexService.downloadFile(targetPath,response);
    }

    @PostMapping("/Index/getNewDynamic")
    @ResponseBody
    public RDynamic getNewDynamic(HttpServletRequest request){//获取自己最新发出的动态
            HttpSession session = request.getSession();
            String email= (String) session.getAttribute("email");
            String username=(String) session.getAttribute("username");
            if(email!=null){
                String DynamicID=request.getParameter("did");
                return webIndexService.getNewDynamic(email,username,DynamicID);
            }
            return null;
    }

    @PostMapping("/Index/getDynamics")
    @ResponseBody
    public List<RDynamic> getDynamics(HttpServletRequest request) throws Exception {//获取所有动态(包括自己的好友)
            HttpSession session = request.getSession();
            String email= (String) session.getAttribute("email");
            String username=(String) session.getAttribute("username");
            if(email!=null){
                return webIndexService.getSortDynamicsByTime(email,username);
            }
            return null;
    }

    @PostMapping("/Index/sendDynamic")
    public String sendDynamic(@RequestParam String content,HttpSession session) throws IOException {//保存自己发出的动态
        String email = (String) session.getAttribute("email");
        if(email!=null) {
            switch (content) {
                case "create" : {//创建动态的保存空间
                    return "forward:/Index/sendDynamic/create";
                }
                case "theme" : {//保存动态的文本内容
                    return "forward:/Index/sendDynamic/writeText";
                }
                case "img" : {//保存动态的图片
                    return "forward:/Index/sendDynamic/upFile";
                }
            }
        }
        return "redirect:/Login";
    }
    @PostMapping("/Index/sendDynamic/create")
    @ResponseBody
    public String createDynamic(HttpServletRequest request) throws IOException {
        String email = (String) request.getSession().getAttribute("email");
        return webIndexService.createDynamic(email);
    }
    @PostMapping("/Index/sendDynamic/writeText")
    public ResponseEntity<Void> writeTextDynamic(HttpServletRequest request) throws IOException {
        String email = (String) request.getSession().getAttribute("email");
        String dynamicID = request.getParameter("did");
        webIndexService.createDynamicText(email, dynamicID, request.getInputStream());
        return ResponseEntity.ok().build(); // 发送HTTP 200 OK响应
    }
    @PostMapping( "/Index/sendDynamic/upFile")
    public ResponseEntity<Void> upFileDynamic(@RequestParam Map<String, MultipartFile> files,HttpServletRequest request) throws IOException {
        String email=(String)request.getSession().getAttribute("email");
        String dynamicID = request.getParameter("did");
        return webIndexService.createDynamicImg(email, dynamicID, files);
    }

    @PostMapping("/Index/deleteDynamic")
    public ResponseEntity<Void> deleteDynamic(HttpServletRequest request){//删除动态
        String dynamicID=request.getParameter("did");
        webIndexService.deleteDynamic(dynamicID);
        return ResponseEntity.ok().build(); // 发送HTTP 200 OK响应
    }

    @PostMapping("/Index/sendEvaluate")
    public String sendEvaluate(@RequestParam String content,HttpServletRequest request) throws IOException {//保存对某个动态的评论
        String email= (String)  request.getSession().getAttribute("email");//获取当前用户的账号
        if(email!=null) {
            switch (content) {
                case "create" : {//创建对某个动态的评论的存储空间
                    return "forward:/Index/sendEvaluate/create";
                }
                case "text" : {//保存对某个动态的评论的文本内容
                    return "forward:/Index/sendEvaluate/writeText";
                }
                case "img" : {//保存对某个动态的评论的图片
                    return "forward:/Index/sendEvaluate/upFile";
                }
            }
        }
        return "redirect:/Login";
    }
    @PostMapping("/Index/sendEvaluate/create")
    @ResponseBody
    public String createEvaluate(HttpServletRequest request){
        String email= (String) request.getSession().getAttribute("email");//获取当前用户的账号
        String dynamicID=request.getParameter("did");
        return webIndexService.createComment(email, dynamicID);
    }
    @PostMapping("/Index/sendEvaluate/writeText")
    public ResponseEntity<Void> writeTextEvaluate(HttpServletRequest request) throws IOException {
        String dynamicID=request.getParameter("did");
        String commentID = request.getParameter("cid");
        webIndexService.createCommentText(dynamicID, commentID, request.getInputStream());
        return ResponseEntity.ok().build(); // 发送HTTP 200 OK响应
    }
    @PostMapping("/Index/sendEvaluate/upFile")
    public ResponseEntity<Void> upFileEvaluate(@RequestParam Map<String, MultipartFile> files,HttpServletRequest request) throws IOException {
        String dynamicID=request.getParameter("did");
        String commentID = request.getParameter("cid");
        return webIndexService.createCommentImg(dynamicID, commentID, files);
    }

    @PostMapping("/Index/getNewComment")//获取自己发出的评论
    @ResponseBody
    public REvaluation getNewComment(@RequestParam String did, @RequestParam String cid, HttpSession session){
        {
            String username=(String) session.getAttribute("username");
            if(username!=null){
                return  webIndexService.getNewComment(username,did,cid);
            }
            return null;
        }
    }

    @PostMapping("/Index/deleteComment")
    public ResponseEntity<Void> deleteComment(@RequestParam String did, @RequestParam String cid){//删除对某个动态的评论
        webIndexService.deleteComment(did,cid);
        return ResponseEntity.ok().build(); // 发送HTTP 200 OK响应
    }
    @RequestMapping("/test")
    public String test(){
        return "test";
    }

    private String getLocalHostAddress(HttpServletRequest request) {
        String scheme = request.getScheme(); // 例如: http
        String serverName = request.getServerName(); // 例如: localhost
        int serverPort = request.getServerPort(); // 例如: 8080
        return scheme+"://"+serverName+":"+serverPort;
    }
}
